package com.ly.anno;

import lombok.val;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

public class ClassDemo {

    public static void main(String[] args) throws
            NoSuchMethodException,
            InvocationTargetException,
            IllegalAccessException {
        A a = new A();
        A b = new B();

        /*a.callTest("张三");//不通过反射调用*/

        showObject(a);
        System.out.println("---------------------------------------");
        showObject(b);
    }

    public static void showObject(Object object) throws
            NoSuchMethodException,
            InvocationTargetException,
            IllegalAccessException {
        Class<?> aClass = object.getClass();
        System.out.println("aClass.getName() = " + aClass.getName());
        System.out.println("aClass.getSimpleName() = " + aClass.getSimpleName());

        for (Field field : aClass.getFields()) {
            System.out.println("field = " + field);
        }

        for (Field declaredField : aClass.getDeclaredFields()) {
            System.out.println("declaredField = " + declaredField);
        }

        Method test = aClass.getDeclaredMethod("test", String.class);
        if(test != null){
            test.setAccessible(true);//强制访问
            test.invoke(object,"张三");
        }

    }
}

class A{
    private int a;
    String b;
    protected Byte c;
    public Date d;

    private void test(int num){

    }

    public void callTest(String s){
        test(s);
    }

    private void test(String s){
        int i = 1/0;
        System.out.println("hello" + s);
    }
}

class B extends A{
    @Override
    public void callTest(String s){
        super.callTest(s);
    }

}